package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    //新增检查组，同时关联检查项
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组，操作t_checkgroup
        checkGroupDao.add(checkGroup);

        //设置检查组和检查项，操作t_checkgroup_checkitem
        //获取检查组id
        Integer checkGroupId = checkGroup.getId();

        //遍历检查项id
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                /*Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId", checkGroupId);
                map.put("checkitemId", checkitemId);

                checkGroupDao.setCheckGroupAndCheckItem(map);*/
                checkGroupDao.setCheckGroupAndCheckItem2(checkGroupId, checkitemId);
            }
        }

        //this.setsetCheckGroupAndCheckItem(checkGroupId, checkitemIds);
    }

    //分页查询检查项
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //分页查询
        PageHelper.startPage(currentPage, pageSize);

        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //根据id查询检查组
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    //根据检查组id查询与其关联的检查项
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    //修改检查组，关联检查项
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改检查组信息
        checkGroupDao.edit(checkGroup);

        //清空检查组关联的检查项
        checkGroupDao.deleteAssoication(checkGroup.getId());

        //重新关联检查组与检查项的关系
        Integer checkGroupId = checkGroup.getId();

        this.setsetCheckGroupAndCheckItem(checkGroupId, checkitemIds);
    }

    //查询所有检查组
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    //建立检查组和检查项之间的关系
    public void setsetCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds){
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                /*Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId", checkGroupId);
                map.put("checkitemId", checkitemId);

                checkGroupDao.setCheckGroupAndCheckItem(map);*/
                checkGroupDao.setCheckGroupAndCheckItem2(checkGroupId, checkitemId);
            }
        }
    }
}
